<template>
  <el-dialog
    v-model="dialogVisible"
    title="消息预览"
    width="80%"
    :fullscreen="false"
    class="message-preview-dialog"
    :close-on-click-modal="false"
    :close-on-press-escape="true"
    destroy-on-close
  >
    <div class="message-preview-container">
      <div class="message-preview-item-list">
        <el-card v-for="(message, index) in messages" :key="index" class="message-preview-item" :class="{ 'is-hover': true }" shadow="hover">
          <div class="preview-header">
            <div class="message-info">
              <span class="message-type-tag" :class="message.type">{{ getMessageTypeLabel(message.type) }}</span>
              <div class="message-content">
                <template v-if="message.type === 'text'">
                  {{ message.text }}
                </template>
                <template v-else-if="message.type === 'image'">
                  <img :src="message.imageUrl" class="preview-image" />
                </template>
                <template v-else-if="message.type === 'video'">
                  <video :src="message.videoUrl" class="preview-video" controls />
                </template>
                <template v-else-if="message.type === 'link'">
                  <div class="link-preview">
                    <div class="link-title">{{ message.linkTitle }}</div>
                    <div class="link-url">{{ message.linkUrl }}</div>
                    <div class="link-desc">{{ message.linkDesc }}</div>
                  </div>
                </template>
              </div>
            </div>
            <div class="preview-actions" v-if="!readonly">
              <div class="action-buttons">
                <el-tooltip content="上移" placement="top">
                  <el-button type="primary" link :disabled="index === 0" @click="handleMoveMessage(index, 'up')">
                    <el-icon><ArrowUp /></el-icon>
                  </el-button>
                </el-tooltip>
                <el-tooltip content="下移" placement="top">
                  <el-button type="primary" link :disabled="index === messages.length - 1" @click="handleMoveMessage(index, 'down')">
                    <el-icon><ArrowDown /></el-icon>
                  </el-button>
                </el-tooltip>
                <el-tooltip content="删除" placement="top">
                  <el-button type="danger" link @click="handleDeleteMessage(index)">
                    <el-icon><Delete /></el-icon>
                  </el-button>
                </el-tooltip>
              </div>
            </div>
          </div>
        </el-card>
      </div>
    </div>
  </el-dialog>
</template>

<script lang="ts">
import { defineComponent, ref, watch } from 'vue';
import { ArrowUp, ArrowDown, Delete } from '@element-plus/icons-vue';

interface MessageData {
  type: 'text' | 'image' | 'video' | 'link';
  data: string;
  text?: string;
  imageUrl?: string;
  videoUrl?: string;
  linkUrl?: string;
  linkTitle?: string;
  linkDesc?: string;
}

export default defineComponent({
  name: 'MessagePreview',
  components: {
    ArrowUp,
    ArrowDown,
    Delete
  },
  props: {
    modelValue: {
      type: Boolean,
      required: true
    },
    messages: {
      type: Array as () => MessageData[],
      required: true
    },
    readonly: {
      type: Boolean,
      default: false
    }
  },
  emits: ['update:modelValue', 'move-message', 'delete-message'],
  setup(props, { emit }) {
    const dialogVisible = ref(props.modelValue);

    watch(
      () => props.modelValue,
      (newVal) => {
        dialogVisible.value = newVal;
      }
    );

    watch(dialogVisible, (newVal) => {
      emit('update:modelValue', newVal);
    });

    const getMessageTypeLabel = (type: MessageData['type']) => {
      const typeMap: Record<MessageData['type'], string> = {
        text: '文本',
        image: '图片',
        video: '视频',
        link: '链接'
      };
      return typeMap[type];
    };

    const handleMoveMessage = (index: number, direction: 'up' | 'down') => {
      emit('move-message', index, direction);
    };

    const handleDeleteMessage = (index: number) => {
      emit('delete-message', index);
    };

    return {
      dialogVisible,
      getMessageTypeLabel,
      handleMoveMessage,
      handleDeleteMessage
    };
  }
});
</script>

<style lang="scss" scoped>
.message-preview-dialog {
  :deep(.el-dialog) {
    max-height: 80vh !important;
    display: flex;
    flex-direction: column;
    border-radius: 8px;
    overflow: hidden;
    box-shadow: 0 4px 24px rgba(0, 0, 0, 0.1);
  }

  :deep(.el-dialog__header) {
    margin: 0;
    padding: 20px 24px;
    border-bottom: 1px solid #ebeef5;
    background: #f5f7fa;
    flex-shrink: 0;

    .el-dialog__title {
      font-size: 18px;
      font-weight: 600;
      color: #303133;
    }
  }
  .message-preview-container {
    height: 100%;
    overflow-y: auto;
    padding-right: 10px;

    &::-webkit-scrollbar {
      width: 6px;
      height: 6px;
    }

    &::-webkit-scrollbar-thumb {
      background-color: #c0c4cc;
      border-radius: 3px;
    }

    &::-webkit-scrollbar-track {
      background-color: #f5f7fa;
    }

    .message-preview-item-list {
      display: flex;
      flex-direction: column;
      gap: 16px;
      padding: 4px;
      min-height: 100%;
    }
  }
}

.message-preview-item {
  transition: all 0.3s ease;
  border: 1px solid #ebeef5;
  border-radius: 8px;
  background: #ffffff;

  &:hover {
    transform: translateY(-2px);
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
  }

  :deep(.el-card__body) {
    padding: 16px !important;
  }

  .preview-header {
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    gap: 16px;

    .message-info {
      flex: 1;
      display: flex;
      align-items: flex-start;
      gap: 12px;

      .message-type-tag {
        padding: 4px 8px;
        border-radius: 4px;
        font-size: 12px;
        font-weight: 500;
        white-space: nowrap;
        flex-shrink: 0;

        &.text {
          background-color: #e6f7ff;
          color: #1890ff;
        }

        &.image {
          background-color: #f6ffed;
          color: #52c41a;
        }

        &.video {
          background-color: #fff7e6;
          color: #fa8c16;
        }

        &.link {
          background-color: #f9f0ff;
          color: #722ed1;
        }
      }

      .message-content {
        flex: 1;
        min-width: 0;
        font-size: 14px;
        color: #606266;
        word-break: break-all;
        white-space: pre-wrap;
        line-height: 1.6;

        .preview-image {
          max-width: 200px;
          max-height: 200px;
          border-radius: 8px;
          box-shadow: 0 2px 8px rgba(0, 0, 0, 0.06);
        }

        .preview-video {
          max-width: 300px;
          max-height: 200px;
          border-radius: 8px;
          box-shadow: 0 2px 8px rgba(0, 0, 0, 0.06);
        }

        .link-preview {
          background: #f8fafc;
          border-radius: 8px;
          padding: 16px;
          border: 1px solid #eef2f6;

          .link-title {
            font-weight: 600;
            color: #2c3e50;
            margin-bottom: 8px;
            font-size: 15px;
          }

          .link-url {
            color: #3b82f6;
            font-size: 13px;
            margin-bottom: 8px;
            word-break: break-all;
          }

          .link-desc {
            color: #64748b;
            font-size: 13px;
            line-height: 1.5;
          }
        }
      }
    }

    .preview-actions {
      flex-shrink: 0;

      .action-buttons {
        display: flex;
        gap: 8px;
        align-items: center;

        .el-button {
          padding: 6px;
          border-radius: 4px;
          transition: all 0.3s;

          &:hover {
            background-color: #f5f7fa;
          }

          &.is-disabled {
            opacity: 0.5;
            cursor: not-allowed;
          }

          .el-icon {
            font-size: 16px;
          }
        }
      }
    }
  }
}

.preview-title {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 16px;
  font-weight: bold;
  margin-bottom: 10px;
  color: #303133;

  .el-button {
    padding: 4px;

    .el-icon {
      font-size: 18px;
    }
  }
}
</style> 